package com.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.domain.LiveEntity;
import com.dto.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.LiveMapper;
import com.service.LiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 直播表 服务实现类
 * </p>
 *
 * @author sly
 * @since 2023-12-23
 */
@Service
public class LiveServiceImpl extends ServiceImpl<LiveMapper, LiveEntity> implements LiveService {

    @Autowired
    private LiveMapper liveMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public PageResult<LiveEntity> pageListLive(LiveEntity live) {
        PageHelper.startPage(live.getCurrentPage(), live.getCurrentSize());
        List<LiveEntity> result = liveMapper.pageListLive(live);
        PageInfo<LiveEntity> pageInfo = new PageInfo<>(result, live.getCurrentSize());
        return new PageResult<>((int) pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public LiveEntity getLiveDetail(Long liveId) {
        // 查询详情
        QueryWrapper<LiveEntity> liveQueryWrapper = new QueryWrapper<>();
        liveQueryWrapper.lambda().eq(LiveEntity::getId, liveId);
        LiveEntity live = liveMapper.selectOne(liveQueryWrapper);
        return live;
    }

    @Override
    public void addLive(LiveEntity live) {
        live.setUserId(1L);
        live.setLiveOnline(1);
        live.setCreateTime(new Date());
        live.setIsDel(0);
        liveMapper.insert(live);
    }

    @Override
    public void updateLive(LiveEntity live) {
        live.setUpdateTime(new Date());
        liveMapper.updateById(live);
    }

    @Override
    public void deleteLive(Long liveId) {
        LiveEntity liveEntity = new LiveEntity();
        liveEntity.setId(liveId);
        liveEntity.setUpdateTime(new Date());
        liveEntity.setIsDel(1);
        liveMapper.updateById(liveEntity);
    }
}
